package com.crm.controller;

import com.crm.entity.*;
import com.crm.entity.Customer;
import com.crm.service.CustomerService;
import com.crm.service.SaleChanceService;
import com.crm.util.DateUtil;
import com.crm.util.ResponseUtil;
import com.crm.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户Controller层
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    /**
     * 分页条件查询客户
     *
     * @param page
     * @param rows
     * @param s_customer
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, Customer s_customer, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("khno", StringUtil.formatLike(s_customer.getKhno()));
        map.put("name", StringUtil.formatLike(s_customer.getName()));
        PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<Customer> customerList = customerService.find(map);

        Long total = customerService.getTotal(map);

        JSONObject result = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(customerList);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }


    /**
     * 添加或者修改客户开发计划
     *
     * @param customer
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    public String save(Customer customer, HttpServletResponse response) throws Exception {
        JSONObject result = new JSONObject();

        if (customer.getId() == null) {
            customer.setKhno("KH" + DateUtil.getCurrentDateStr()); // 动态生成客户编号
            customerService.add(customer);
        } else {
            customerService.update(customer);
        }

        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 删除客户
     *
     * @param id
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "id") String id, HttpServletResponse response) throws Exception {
        customerService.delete(Integer.parseInt(id));
        JSONObject result = new JSONObject();
        result.put("success", true);       //  result.put("isError", true);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 通过ID查找实体
     *
     * @param id
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/findById")
    public String findById(@RequestParam(value = "id") String id, HttpServletResponse response) throws Exception {
        Customer customer = customerService.findById(Integer.parseInt(id));
        JSONObject jsonObject = JSONObject.fromObject(customer);
        ResponseUtil.write(response, jsonObject);
        return null;
    }


    /**
     * 分页条件查询客户
     *
     * @param s_customer
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/comboList")
    public String comboList(Customer s_customer, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("khno", StringUtil.formatLike(s_customer.getKhno()));
        map.put("name", StringUtil.formatLike(s_customer.getName()));
        List<Customer> customerList = customerService.find(map);

        JSONArray jsonArray = JSONArray.fromObject(customerList);
        ResponseUtil.write(response, jsonArray);
        return null;
    }

    /**
     * 查询客户贡献分析
     *
     * @param page
     * @param rows
     * @param name
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/findCustomerGx")
    public String findCustomerGx(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, String name, HttpServletResponse response) throws Exception {
        PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", StringUtil.formatLike(name));
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());

        List<CustomerGx> customerGxList = customerService.findCustomerGx(map);
        Long total = customerService.getTotalCustomerGx(map);

        JSONObject result = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(customerGxList);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 查询客户贡献分析
     *
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/findCustomerGc")
    public String findCustomerGc(HttpServletResponse response) throws Exception {
        List<CustomerGc> customerGxList = customerService.findCustomerGc();

        JSONArray jsonArray = JSONArray.fromObject(customerGxList);
        ResponseUtil.write(response, jsonArray);
        return null;
    }

    /**
     * 查询客户服务分析
     *
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/findCustomerFw")
    public String findCustomerFw(HttpServletResponse response) throws Exception {
        List<CustomerFw> customerFwList = customerService.findCustomerFw();

        JSONArray jsonArray = JSONArray.fromObject(customerFwList);
        ResponseUtil.write(response, jsonArray);
        return null;
    }
}
